﻿/*
    form.js Last update 14/03/2014

    By Soravit Pornpaiboonstid
	
	Gen form.js Last update 14/05/2014 by Weerachai

    
    Fix bug delete child items. (At line 195)

    Log
    01/02/2014 - Fix bug delete child items.
    05/02/2014 - Fix select item after search on IE and Chrome
*/

$.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase());

$(function () {

    $.datepicker.setDefaults($.datepicker.regional["th"]);

});

function popitup(url, windowName, key) {
    if (key != "") { url = url + "?AutoSearch=" + key; }
    newwindow = window.open(url, windowName, 'height=600,width=500,scrollbars=yes');
    if (window.focus) { newwindow.focus() }
    return false;
}

function popupDialog(url, windowName, required, key) {
    if (required != "") { url = url + "?required=" + required; }
    if (key != "") { url = url + "&AutoSearch=" + key; }
    var r = window.showModalDialog(url, windowName, "dialogWidth:670px;dialogHeight:600px;");
    if (typeof r != 'undefined') {
        return r;
    }
    else {
        return window.customReturnValue;
    }

}

function BindingValueFromGetLookUp(returnValue) {

    if (returnValue != null) {
        var jsonString = jQuery.parseJSON(returnValue);

        for (var key in jsonString) {
            if (jsonString.hasOwnProperty(key)) {
                $("#" + key).val(jsonString[key]);
            }
        }
    }
}

function BindingValueFromPopLookUp(returnValue) {
    if (returnValue != null) {
        for (var key in returnValue) {
            //if (returnValue.hasOwnProperty(key)) {
            //    $("#" + key).val(returnValue[key]);
            //}
            if (returnValue[key] != undefined) {
                $("#" + key).val(returnValue[key]);
            }
        }
    }
}

function SetDatePicker(controlId, maxdate, mindate, yearrange) {

    var obj = '#' + controlId;
    $(obj).attr('readonly', true);
    $(obj).datepicker({
        buttonImage: '@Url.Content("~/Content/images/datepicker.png")',
        changeMonth: true, changeYear: true,
        defaultDate: new Date(),
        maxDate: maxdate,
        minDate: mindate,
        yearRange: yearrange,
    });
}

function SetLookupFor(controlId, getLookupId, popLokupId) {

    var GetLookup_url = $("#" + getLookupId).val();
    var PopLookup_url = $("#" + popLokupId).val();

    var obj = '#' + controlId;
    var txtCode = obj + 'LookupField';
    var txtName = obj + 'DescField';
    var btnObj = obj + 'ShowAll';

    $(txtCode).change(function () {
        var searchKey = $(txtCode).val();
        //alert(searchKey);
        $.ajax({
            url: GetLookup_url,
            type: 'GET',
            data: 'AutoSearch=' + searchKey,
            success: function (result) {
                if (result == "NotFound") {
                    $(obj).val("");
                    $(txtCode).val("");
                    $(txtName).val("");
                    popitup(PopLookup_url, obj, "");
                }
                else if (result == "MoreOne") {
                    $(obj).val("");
                    $(txtCode).val("");
                    $(txtName).val("");
                    popitup(PopLookup_url, obj, searchKey);
                }
                else {
                    var Binding = result.split("|");
                    $(obj).val(Binding[0]);
                    $(txtCode).val(Binding[1]);
                    $(txtName).val(Binding[2]);
                }
            },
            fail: function (result) {
                $(obj).val("");
                $(txtCode).val("");
                $(txtName).val("");
                popitup(PopLookup_url, obj, "");
            }
        });
    });

    $(document).on("click", btnObj, function (e) {
        e.preventDefault();
        popitup(PopLookup_url, obj, "");
    });
}

function SetExternalLookupFor(controlId, getLookupId, popLokupId, requiredFileds) {
    var GetLookup_url = $("#" + getLookupId).val();
    var PopLookup_url = $("#" + popLokupId).val();

    var obj = '#' + controlId;
    var txtCode = obj + 'LookupField';
    //var txtName = obj + 'DescField';
    var btnObj = obj + 'ShowAll';

    $(txtCode).change(function () {
        var searchKey = $(txtCode).val();
        //alert(searchKey);
        $.ajax({
            url: GetLookup_url,
            type: 'GET',
            data: 'AutoSearch=' + searchKey + '&required=' + requiredFileds,
            success: function (result) {
                if (result == "NotFound") {
                    $(obj).val("");
                    $(txtCode).val("");
                    //$(txtName).val("");
                    var r = popupDialog(PopLookup_url, obj, requiredFileds, "");
                    BindingValueFromGetLookUp(r);
                }
                else if (result == "MoreOne") {
                    $(obj).val("");
                    $(txtCode).val("");
                    //$(txtName).val("");
                    var r = popupDialog(PopLookup_url, obj, requiredFileds, searchKey);
                    BindingValueFromGetLookUp(r);
                }
                else {
                    BindingValueFromGetLookUp(result);
                }
            },
            fail: function (result) {
                $(obj).val("");
                $(txtCode).val("");
                //$(txtName).val("");
                var r = popupDialog(PopLookup_url, obj, requiredFileds, "");
                BindingValueFromGetLookUp(r);
            }
        });
    });

    $(document).on("click", btnObj, function (e) {
        e.preventDefault();
        var r = popupDialog(PopLookup_url, obj, requiredFileds, "");
        BindingValueFromPopLookUp(r);
    });
}

function SetOneToManyFor(controlId, oneToManyActionUrl, baseActionUrl, mainFormId) {
    $("#" + controlId + "-confirm-delete").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        buttons: {
            Yes: function () {
                $(this).dialog("close");
                $.post(oneToManyActionUrl, $("#" + mainFormId).serialize(), function (data) {
                    window.location.href = baseActionUrl; //+ '&result=' + data + '&operation=delete';
                });
            },
            No: function () {
                $(this).dialog("close");
            }
        }
    });

    $("#" + controlId + "-dialog-alert").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });

    $(document).on("click", "#" + controlId + "-button-delete", function (e) {
        var checkboxes = $('#' + controlId + ' input:checkbox');
        if (checkboxes.size() == 0)
            $("#" + controlId + "-dialog-alert").dialog("open");
        else {
            var isCheck = false;
            for (i = 0; i < checkboxes.size() ; i++) {
                isCheck = checkboxes[i].checked;
                if (isCheck)
                    break;
            }

            if (!isCheck) {
                $("#" + controlId + "-dialog-alert").dialog("open");
            }
            else {
                $("#" + controlId + "-confirm-delete").dialog("open");

            }
        }
    });

    $('#' + controlId + ' th').each(function () {
        if (this.innerHTML == "") {
            this.innerHTML = "<input type='checkbox' id='chkHeader'  />";
        }
    });

    $('#' + controlId + ' th input:checkbox').click(function (e) {
        var table = $(e.target).closest('table');
        $('td input:checkbox', table).attr('checked', e.target.checked);
    });
}